package com.dstz.bpm.engine.plugin.cmd;

import com.dstz.base.api.exception.BusinessException;
import com.dstz.bpm.api.constant.EventType;
import com.dstz.bpm.api.engine.action.cmd.TaskActionCmd;
import com.dstz.bpm.api.engine.plugin.cmd.TaskCommand;
import com.dstz.bpm.api.engine.plugin.context.BpmPluginContext;
import com.dstz.bpm.api.engine.plugin.def.BpmExecutionPluginDef;
import com.dstz.bpm.api.engine.plugin.def.BpmPluginDef;
import com.dstz.bpm.api.exception.BpmStatusCode;
import com.dstz.bpm.api.model.nodedef.BpmNodeDef;
import com.dstz.bpm.api.service.BpmProcessDefService;
import com.dstz.bpm.engine.model.DefaultBpmProcessDef;
import com.dstz.bpm.engine.plugin.factory.BpmPluginFactory;
import com.dstz.bpm.engine.plugin.factory.BpmPluginSessionFactory;
import com.dstz.bpm.engine.plugin.runtime.BpmExecutionPlugin;
import com.dstz.bpm.engine.plugin.session.BpmExecutionPluginSession;
import java.util.Iterator;
import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

@Service
public class TaskPluginCommand implements TaskCommand {
   protected Logger LOG = LoggerFactory.getLogger(this.getClass());
   @Resource
   BpmProcessDefService a;

   public void execute(EventType eventType, TaskActionCmd actionModel) {
      this.LOG.debug("↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓【{}】插件执行开始↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓", eventType.getValue());
      String defId = actionModel.getBpmTask().getDefId();
      BpmNodeDef bpmNodeDef = this.a.getBpmNodeDef(defId, actionModel.getNodeId());
      BpmExecutionPluginSession bpmTaskSession = BpmPluginSessionFactory.buildTaskPluginSession(actionModel, eventType);
      this.LOG.trace("============【节点插件】========{}=========", bpmNodeDef.getBpmPluginContexts().size());
      Iterator var6 = bpmNodeDef.getBpmPluginContexts().iterator();

      BpmExecutionPlugin bpmExecutionPlugin;
      while(var6.hasNext()) {
         BpmPluginContext bpmPluginContext = (BpmPluginContext)var6.next();
         BpmPluginDef bpmPluginDef = bpmPluginContext.getBpmPluginDef();
         if (bpmPluginDef instanceof BpmExecutionPluginDef) {
            bpmExecutionPlugin = BpmPluginFactory.buildExecutionPlugin(bpmPluginContext, eventType);
            if (bpmExecutionPlugin != null) {
               try {
                  this.LOG.debug("==>执行任务插件【{}】", bpmPluginContext.getTitle());
                  bpmExecutionPlugin.execute(bpmTaskSession, bpmPluginDef);
               } catch (Exception var12) {
                  this.LOG.error("{}执行任务插件【{}】出现异常:{}", new Object[]{actionModel.getNodeId(), bpmPluginContext.getTitle(), var12.getMessage(), var12});
                  throw new BusinessException(bpmPluginContext.getTitle(), BpmStatusCode.PLUGIN_ERROR, var12);
               }
            }
         }
      }

      DefaultBpmProcessDef bpmProcessDef = (DefaultBpmProcessDef)this.a.getBpmProcessDef(defId);
      this.LOG.trace("============【全局插件】========{}=========", bpmProcessDef.getBpmPluginContexts().size());
      Iterator var14 = bpmProcessDef.getBpmPluginContexts().iterator();

      while(var14.hasNext()) {
         BpmPluginContext globalCtx = (BpmPluginContext)var14.next();
         bpmExecutionPlugin = BpmPluginFactory.buildExecutionPlugin(globalCtx, eventType);
         if (bpmExecutionPlugin != null) {
            try {
               this.LOG.debug("==>【{}】节点执行全局插件【{}】", bpmNodeDef.getName(), globalCtx.getTitle());
               bpmExecutionPlugin.execute(bpmTaskSession, globalCtx.getBpmPluginDef());
            } catch (Exception var11) {
               this.LOG.error("【{}】节点执行全局插件【{}】出现异常:{}", new Object[]{bpmNodeDef.getName(), globalCtx.getTitle(), var11.getMessage(), var11});
               throw new BusinessException(globalCtx.getTitle(), BpmStatusCode.PLUGIN_ERROR, var11);
            }
         }
      }

      this.LOG.debug("↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑【{}】插件执行完毕↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑", eventType.getValue());
   }
}
